package basic.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/31 10:53
 */
public class XM40 {

    public static void main(String[] args) throws IOException {
        // 拿到所有参数
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int re = Integer.parseInt(bf.readLine());
        int a[] = new int[re];
        int b[] = new int[re];
        int max = 0;
        int maxindex = 0;
        int youmaxindex = 0;
        for (int i = 0; i < a.length; i++) {
            a[i] = Integer.parseInt(bf.readLine());
            b[i] = a[i];
            if (a[i] >= max) {
                if (a[i] > max) {
                    youmaxindex = i;
                    maxindex = i;
                } else youmaxindex = i;
                max = a[i];
            }
        }
        int m = Integer.parseInt(bf.readLine());

        // 计算结果
        int max1 = 0;
        int pre = a[0];
        long total = 0;
        // 计算最大高度左边的水容量
        for (int i = 1; i < maxindex; i++) {
            if (a[i] < pre) {
                total += pre - a[i];
                a[i] = pre;
            } else {
                pre = a[i];
            }
        }
        // 计算最大容量右边的水容量
        pre = a[re - 1];
        for (int i = re - 2; i > maxindex; i--) {
            if (a[i] < pre) {
                total += pre - a[i];
                a[i] = pre;
            } else {
                pre = a[i];
            }
        }
        // 计算增加m后的结果
        for (int i = 0; i < maxindex; i++) {
            int tem = 0;
            // 在最左边加m
            for (int j = i + 1; j < maxindex; j++) {
                if (Math.min(a[i] + m, max) >= a[j]) {
                    tem += (Math.min(a[i] + m, max) - a[j]);
                } else break;
            }
            int tindex = youmaxindex;
            if (a[i] + m > max) {
                int s = ((a[i] + m) - max) / 2;
                int kk = m;
                int tt = s * (youmaxindex - i - 1);
                int tt1 = 0;
                for (int j = youmaxindex + 1; j < re; j++) {
                    if (kk - (2 * max - a[i] - a[j]) > 0) {
                        int a1 = ((kk - (2 * max - a[i] - a[j])) / 2) * (j - i - 1);
                        if (((kk - (2 * max - a[i] - a[j])) / 2) * (j - i - 1) + tt1 > tt) {
                            tt = ((kk - (2 * max - a[i] - a[j])) / 2) * (j - i - 1) + tt1;
                            tindex = j;
                        }
                        tt1 += (max - a[j]);
                    }
                }
                tem += tt;
                int t1 = 0;
                int t2 = 0;
                if ((kk - (2 * max - a[i] - a[tindex])) % 2 != 0) {
                    for (int j = tindex + 1; j < re - 1; j++) {
                        if (a[j] == a[j + 1]) {
                            ++t1;
                            if (j == re - 2) {
                                t2 = Math.max(t2, t1);
                            }
                        } else {
                            t2 = Math.max(t2, t1);
                            t1 = 0;
                        }
                    }
                    t1 = 0;
                    for (int j = 0; j < i; j++) {
                        if (a[j] == a[j + 1]) {
                            ++t1;
                            if (j == i - 1) {
                                t2 = Math.max(t2, t1);
                            }
                        } else {
                            t2 = Math.max(t2, t1);
                            t1 = 0;
                        }
                    }
                    tem += t2;
                }
            }
            max1 = Math.max(max1, tem);
        }
        for (int i = re - 1; i > maxindex; i--) {
            int tem = 0;
            for (int j = i - 1; j > maxindex; j--) {
                if (Math.min(a[i] + m, max) >= a[j])
                    tem += (Math.min(a[i] + m, max) - a[j]);
                else break;
            }
            int tindex = maxindex;
            if (a[i] + m > max) {
                int s = ((a[i] + m) - max) / 2;
                int kk = m;
                int tt = s * (i - maxindex - 1);
                int tt1 = 0;
                for (int j = maxindex - 1; j >= 0; j--) {
                    if (kk - (2 * max - a[i] - a[j]) > 0) {
                        int a1 = ((kk - (2 * max - a[i] - a[j])) / 2) * (i - j - 1);
                        if (((kk - (2 * max - a[i] - a[j])) / 2) * (j - i - 1) + tt1 > tt) {
                            tt = ((kk - (2 * max - a[i] - a[j])) / 2) * (j - i - 1) + tt1;
                            tindex = j;
                        }
                        tt1 += (max - a[j]);
                    }
                }
                tem += tt;
                int t1 = 0;
                int t2 = 0;
                if ((kk - (2 * max - a[i] - a[tindex])) % 2 != 0) {
                    for (int j = tindex - 1; j > 0; j--) {
                        if (a[j] == a[j - 1]) {
                            ++t1;
                            if (j == 1) {
                                t2 = Math.max(t2, t1);
                            }
                        } else {
                            t2 = Math.max(t2, t1);
                            t1 = 0;
                        }
                    }
                    t1 = 0;
                    for (int j = i + 1; j < re - 1; j++) {
                        if (a[j] == a[j + 1]) {
                            ++t1;
                            if (j == re - 2) {
                                t2 = Math.max(t2, t1);
                            }
                        } else {
                            t2 = Math.max(t2, t1);
                            t1 = 0;
                        }
                    }
                    tem += t2;
                }
            }
            max1 = Math.max(max1, tem);

        }
        System.out.println(total + max1);
    }
}
